/**
 * 
 */
package com.xbang.attendance.web.action.purchase.purchaselist;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.order.PurchaseOrder;
import com.xbang.attendance.service.PurchaseOrderService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.struts.support.BaseActionSupport;

/**
 * @author dev.zhang
 * 
 */
public class PurchaseMananger extends BaseActionSupport {

	private static final long serialVersionUID = -1532252860400494843L;

	
	private String exportFile;
	private Map<String, String> fieldsMap;
	private String[] fields;
	private Integer id;
	private String ids;
	private Integer currPageId;

	public Integer getCurrPageId() {
		return currPageId;
	}

	public void setCurrPageId(Integer currPageId) {
		this.currPageId = currPageId;
	}

	@Autowired
	private PurchaseOrderService purchaseOrderService;

	private List<PurchaseOrder> purchaseOrders = new ArrayList<PurchaseOrder>();

	@Action("/purchase/purchaselist/finish/${id}")
	public String finish() throws Exception {

		if (id != null) {
			PurchaseOrder purchaseOrder = purchaseOrderService.getPurchaseOrderByKey(id);

			if (purchaseOrder != null) {
				purchaseOrder.setPercentage(new BigDecimal(100));
				purchaseOrderService.update(purchaseOrder);
			}

		}
		
		return "r:/purchase/purchaselist/index/"+currPageId;
	}
	
	/**
	 * 删除purchaseOrder
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/purchase/purchaselist/delete/${id}")
	public String delete() throws Exception {
		purchaseOrderService.deletePurchaseOrder(id);

		return "r:/purchase/purchaselist/index/" + currPageId;
	}


	@Action("/purchase/purchaselist/toexport")
	public String toExport() throws Exception {
		// setFieldsMap(FieldUtil.pofields);
		return "field";
	}

	@Action("/purchase/purchaselist/export")
	public String export() throws Exception {
		String webPath = ServletActionContext.getRequest().getRealPath("//download//order//purchase");
		HttpServletRequest request = ServletActionContext.getRequest();
		String urlPath = request.getRequestURL().toString();
		String contextPath = request.getContextPath();
		String contextURL = urlPath.split(contextPath)[0] + contextPath;
		exportFile = contextURL + "/download/order/purchase/" + purchaseOrderService.genericExcelFile(webPath, fields);
		return "r:" + exportFile;
	}

	@Action("/purchase/purchaselist/deletes")
	public String deletePurchase() throws Exception {
		System.out.println("====="+currPageId);
		if (ids != null && ids.length() > 0) {
			String[] idss = ids.split(",");
			for (String string : idss) {
				Integer purchaseId = Integer.valueOf(string);
				purchaseOrderService.deletePurchaseOrder(purchaseId);
			}

		}
		return "r:/purchase/purchaselist/index/" + currPageId;
	}

	@Action("/purchase/purchaselist/exportpurchase")
	public String exportPurchase() throws Exception {
		String webPath = ServletActionContext.getRequest().getRealPath("//download//order//purchase");
		HttpServletRequest request = ServletActionContext.getRequest();
		String urlPath = request.getRequestURL().toString();
		String contextPath = request.getContextPath();
		String contextURL = urlPath.split(contextPath)[0] + contextPath;

		String[] idss = ids.split(",");

		exportFile = contextURL + "/download/order/purchase/" + purchaseOrderService.genericExcelFile(webPath, ids, idss);

		return "r:" + exportFile;
	}

	public List<PurchaseOrder> getPurchaseOrders() {
		return purchaseOrders;
	}

	public void setPurchaseOrders(List<PurchaseOrder> purchaseOrders) {
		this.purchaseOrders = purchaseOrders;
	}

	/**
	 * @return the exportFile
	 */
	public String getExportFile() {
		return exportFile;
	}

	/**
	 * @param exportFile
	 *            the exportFile to set
	 */
	public void setExportFile(String exportFile) {
		this.exportFile = exportFile;
	}

	public Map<String, String> getFieldsMap() {
		return fieldsMap;
	}

	public void setFieldsMap(Map<String, String> fieldsMap) {
		this.fieldsMap = fieldsMap;
	}

	public String[] getFields() {
		return fields;
	}

	public void setFields(String[] fields) {
		this.fields = fields;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

}
